package com.sinoglobal.sinostore.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.BaseActivity;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.ViewHolder;
import com.sinoglobal.sinostore.bean.AddressVo;
import com.sinoglobal.sinostore.bean.BaseVo;
import com.sinoglobal.sinostore.bean.IndentDetailListVo;
import com.sinoglobal.sinostore.bean.OrderDetailVo;
import com.sinoglobal.sinostore.bean.OrderVo;
import com.sinoglobal.sinostore.bean.ServiceTelVo;
import com.sinoglobal.sinostore.dialog.RefundDialog;
import com.sinoglobal.sinostore.dialog.RefundDialog.Listener;
import com.sinoglobal.sinostore.dialog.SweetAlertDialog;
import com.sinoglobal.sinostore.dialog.SweetAlertDialog.OnSweetClickListener;
import com.sinoglobal.sinostore.listener.OnAntiViolenceClickListener;
import com.sinoglobal.sinostore.service.OrderManager;
import com.sinoglobal.sinostore.service.OrderManager.OrderOperationCallBack;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.utils.BitmapHelp;
import com.sinoglobal.sinostore.utils.TextUtil;
import com.sinoglobal.sinostore.utils.TimeUtil;

/**
 * 
 * @author lk
 * @createdate 2014-9-16 下午12:36:33
 * @Description: TODO(订单详情)
 * 
 * 
 */
public class OrderDetailActivity extends StateBaseActivity implements Listener {
	private OrderVo orderVo;// 订单实体
	private IndentDetailListVo indentDetailListVo;// 订单详情实体
	private TextView goodsTitle;// 商品名称
	private TextView tvFreight;// 运费
	private TextView tvBuyNum;// 购买数量
	private TextView tvPrice;// 商品单价
	private TextView tvTotalPrice;// 订单总价
	private TextView tvIndentNum;// 订单编号
	private TextView tvIndentState;// 订单状态
	private TextView tvConvertDate;// 兑换日期
	private TextView tvConvertMethod;// 领取方式
	// private TextView tvSureDate;//确认收货时间
	private TextView tvExpressName;// 快递公司
	private TextView tvWaybillNum;// 快递单号
	private TextView tvNote;// 留言
	private TextView tvInvoiceTitle;// 发票信息
	private ImageView goodsImg;// 商品图片
	private TextView btnOperation;// 底部操作按钮
	private TextView btnCancel;// 取消
	private TextView tvAvailableTime;// 使用有效期 虚拟商品有该项
	private TextView btnLookalladdress;// 查看更多自取地址

	private LinearLayout llAvailableTime;// 使用有效期 虚拟商品有该项

	private RelativeLayout llGoodsFreight;// 实物 运费信息
	private LinearLayout llGoodsReal;// 实物信息
	private LinearLayout llExpressInfo;// 实物快递信息
	private LinearLayout llAddressInfo;// 地址信息
	private RelativeLayout rlLookziqu;// 查看自取地址
	private LinearLayout llOrderOption;// 处理订单按钮的布局

	View goodsInfoLayout;// 商品信息
	RefundDialog refundDialog;// 退款对话框
	private SweetAlertDialog sweetAlertDialog;

	/** 支付方式 **/
	private int payType;

	private View tempView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		titleView.setText("订单详情");
		setContentView(R.layout.shop_activity_order_detail);
		openChenJInShi();
		mainBody.setVisibility(View.INVISIBLE);
		orderVo = (OrderVo) getIntent().getSerializableExtra(OrderVo.class.getCanonicalName());
		sweetAlertDialog = new SweetAlertDialog(this);
		// 初始化控件
		initView();
		loadData(orderVo);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onRestart() {
		super.onRestart();

	}

	private void initView() {
		goodsTitle = (TextView) this.findViewById(R.id.commodity_tv_title);
		goodsInfoLayout = findViewById(R.id.goodsInfoLayout);
		tvBuyNum = (TextView) this.findViewById(R.id.num);
		tvPrice = (TextView) this.findViewById(R.id.price);
		tvFreight = (TextView) this.findViewById(R.id.freight);
		tvTotalPrice = (TextView) this.findViewById(R.id.totalprice);
		tvIndentNum = (TextView) this.findViewById(R.id.indentNum);
		tvIndentState = (TextView) this.findViewById(R.id.indent_state);
		tvConvertDate = (TextView) this.findViewById(R.id.convert_date);
		tvConvertMethod = (TextView) this.findViewById(R.id.convert_method);
		// tvSureDate = (TextView)this.findViewById(R.id.sureDate);
		tvExpressName = (TextView) this.findViewById(R.id.express_name);
		tvWaybillNum = (TextView) this.findViewById(R.id.waybill_num);
		tvNote = (TextView) this.findViewById(R.id.note);
		tvAvailableTime = (TextView) this.findViewById(R.id.useAvailableTime);
		llAvailableTime = (LinearLayout) this.findViewById(R.id.llUseAvailableTime);
		tvInvoiceTitle = (TextView) this.findViewById(R.id.invoice_title);
		goodsImg = (ImageView) this.findViewById(R.id.commodity_iv_img);

		llOrderOption = (LinearLayout) this.findViewById(R.id.ll_order_option);
		btnOperation = (TextView) this.findViewById(R.id.btnOperation);
		btnCancel = (TextView) this.findViewById(R.id.shop_detail_btn_cancel_indent);
		btnCancel.setVisibility(View.GONE);

		llGoodsFreight = (RelativeLayout) findViewById(R.id.llGoodsFreight);
		llGoodsReal = (LinearLayout) findViewById(R.id.llGoodsReal);
		llExpressInfo = (LinearLayout) findViewById(R.id.llExpressInfo);
		llAddressInfo = (LinearLayout) this.findViewById(R.id.ll_address_info);
		rlLookziqu = (RelativeLayout) this.findViewById(R.id.rl_lookziqu);
		btnLookalladdress = (TextView) this.findViewById(R.id.btn_lookalladdress);

		btnOperation.setOnClickListener(clickListener);
		btnCancel.setOnClickListener(clickListener);
		goodsImg.setOnClickListener(clickListener);
		goodsTitle.setOnClickListener(clickListener);
		goodsInfoLayout.setOnClickListener(clickListener);
		btnLookalladdress.setOnClickListener(clickListener);
	}

	private void setView(final OrderVo orderVo, IndentDetailListVo result) {
		btnCancel.setVisibility(View.GONE);
		OrderDetailVo detailVo = null;
		if (Constants.STORE_SUCCESS_CODE.equals(result.getCode())) {
			Constants.PIC_HOST = result.getHost();
			// indentDetailLayout.setVisibility(View.VISIBLE);
			mainBody.setVisibility(View.VISIBLE);
			detailVo = result.getInfo();
			if (null != detailVo) {
				// orderDetails.setVisibility(View.VISIBLE);
				goodsTitle.setText(detailVo.getGoods_name());
				llOrderOption.setVisibility(View.VISIBLE);
				tvIndentNum.setText("订单编号：" + detailVo.getOrder_num());
				if (TextUtil.isZeroOrNull(detailVo.getSpending()) && !TextUtil.isZeroOrNull(detailVo.getCash_spend())) {// 积分单价为零，现金不为零，为现金支付
					tvPrice.setText(String.format("现金%s元", detailVo.getCash_spend()));
				} else if (TextUtil.isZeroOrNull(detailVo.getCash_spend()) && !TextUtil.isZeroOrNull(detailVo.getSpending())) {// 现金单价为零，为积分支付
					tvPrice.setText(String.format("%s%s", detailVo.getSpending(), getString(R.string.score_unit)));
				} else {// 两个都不为零则为现金加积分支付
					tvPrice.setText(String.format("现金%s元+%s%s", detailVo.getCash_spend(), detailVo.getSpending(),
							getString(R.string.score_unit)));
				}
				tvBuyNum.setText(String.format("x%s", detailVo.getGoods_nums()));
				tvFreight.setText(String.format("%s元", detailVo.getFreight()));

				if (TextUtil.isZeroOrNull(detailVo.getSpending_total()) && !TextUtil.isZeroOrNull(detailVo.getCash_pay_total())) {// 积分总价为空,现金不为零，为现金支付
					double tPrice = Double.parseDouble(detailVo.getCash_pay_total());
					if (!TextUtil.isZeroOrNull(detailVo.getFreight())) {
						tPrice += Double.parseDouble(detailVo.getFreight());
					}
					tvTotalPrice.setText(String.format("现金%.2f元", tPrice));
					/** 现金支付 **/
					payType = Constants.MONEYPAY;
				} else if (TextUtil.isZeroOrNull(detailVo.getCash_pay_total()) && !TextUtil.isZeroOrNull(detailVo.getSpending_total())
						&& TextUtil.isZeroOrNull(detailVo.getFreight())) {// 现金总价为空，积分不为空，为积分支付
					tvTotalPrice.setText(String.format("%s%s", detailVo.getSpending_total(), getString(R.string.score_unit)));
					/** 积分支付 **/
					payType = Constants.SCOREPAY;
				} else {//
					double tPrice = Double.parseDouble(detailVo.getCash_pay_total());
					if (!TextUtil.isZeroOrNull(detailVo.getFreight())) {
						tPrice += Double.parseDouble(detailVo.getFreight());
					}
					tvTotalPrice
							.setText(String.format("现金%.2f元+%s%s", tPrice, detailVo.getSpending_total(), getString(R.string.score_unit)));
					/** 现金支付 **/
					payType = Constants.MONEYPAY;
				}

				String state = "已发货";
				// 订单状态：1=未付款 2=未发货+未领取（支付完成）
				// 3=已发货 4=已收货+已领取（待评价） 5=已完成（评价过的）
				// 6=已取消（指退款的订单） 7=已过期 8 已关闭（已关闭包含a，退款成功的 b，未付款取消的订单）
				switch (Integer.parseInt(detailVo.getStatus())) {
				case Constants.UNPAID_CODE:
					state = "待付款";
					btnOperation.setText("立即付款");
					btnCancel.setText("取消订单");
					llAvailableTime.setVisibility(View.GONE);// 待付款状态不显示有效期
					btnCancel.setVisibility(View.VISIBLE);// 隐藏其他状态操作的按钮
					break;
				case Constants.UNFILLEDORDER_CODE:// 2=未发货+未领取（支付完成）
					state = "未领取";
					btnOperation.setText("申请退款");
					if (Constants.VIRTUALGOODS.equals(orderVo.getType())) {// 2
																			// 为虚拟商品
						state = "未使用";
					} else {
						if (Constants.ZIQU.equals(orderVo.getDraw_type())) {// 自取
							state = "未领取";
						} else {// 快递
							state = "待发货";
						}
					}
					break;
				case Constants.SHIPPED_CODE:// 3=已发货
					state = "已发货";
					btnOperation.setVisibility(View.VISIBLE);
					btnOperation.setText("确认收货");
					btnCancel.setText("申请退款");
					btnCancel.setVisibility(View.VISIBLE);// 隐藏其他状态操作的按钮
					break;
				case Constants.TAKEGOODS_CODE:// 4=已收货+已领取（待评价
					if (Constants.VIRTUALGOODS.equals(orderVo.getType())) {
						state = "已使用";
					} else {
						if (Constants.ZIQU.equals(orderVo.getDraw_type())) {
							state = "已领取";
						} else {
							state = "待评价";
						}
					}
					btnOperation.setVisibility(View.VISIBLE);
					btnOperation.setText("评价");
					if (Constants.VIRTUALGOODS.equals(orderVo.getType())) {// 2
																			// 为虚拟商品
						btnCancel.setVisibility(View.GONE);
						;
					} else {
						btnCancel.setText("申请退款");
						btnCancel.setVisibility(View.VISIBLE);
					}
					break;
				case Constants.FINISH_CODE:// 5=已完成（评价过的）
					state = "已完成";
					btnCancel.setVisibility(View.VISIBLE);
					btnCancel.setText("申请退款");
					btnOperation.setVisibility(View.VISIBLE);
					btnOperation.setText("删除订单");
					break;
				case Constants.CANCEL_CODE:// 6=已取消（指退款的订单）
					state = "已取消";
					btnOperation.setVisibility(View.VISIBLE);
					btnOperation.setText("删除订单");
					break;
				case Constants.PAST_CODE:// 7=已过期
					state = "已过期";
					btnOperation.setVisibility(View.VISIBLE);
					btnOperation.setText("删除订单");
					btnCancel.setText("申请退款");
					btnCancel.setVisibility(View.VISIBLE);
					break;
				case Constants.CLOSE_CODE:// 8 已关闭（已关闭包含a，退款成功的 b，未付款取消的订单）
					state = "已关闭";
					btnOperation.setVisibility(View.VISIBLE);
					btnOperation.setText("删除订单");
					break;
				case Constants.EXCEPTION_CODE:// 异常订单
					state = "异常订单";
					llOrderOption.setVisibility(View.GONE);
					break;
				default:
					break;
				}
				tvIndentState.setText(state);
				tvConvertDate.setText("购买日期：" + TimeUtil.praseStringtoString(detailVo.getCreate_time()));
				if (TextUtils.isEmpty(detailVo.getExpress_name())) {
					tvExpressName.setText("暂无");
				} else {
					tvExpressName.setText(detailVo.getExpress_name());
				}
				if (TextUtils.isEmpty(detailVo.getWaybill_num())) {
					tvWaybillNum.setText("暂无");
				} else {
					tvWaybillNum.setText(detailVo.getWaybill_num());
				}
				if (TextUtils.isEmpty(detailVo.getNote())) {
					tvNote.setText("暂无");
				} else {
					tvNote.setText(detailVo.getNote());
				}
				// 发票信息
				if (TextUtils.isEmpty(detailVo.getInvoice_title())) {
					tvInvoiceTitle.setText("暂无");
				} else {
					tvInvoiceTitle.setText(detailVo.getInvoice_title());
				}

				if (Constants.VIRTUALGOODS.equals(orderVo.getType())) {// 2
																		// 为虚拟商品
					llGoodsReal.setVisibility(View.GONE);
					llGoodsFreight.setVisibility(View.GONE);
					tvAvailableTime.setText("使用有效期：" + orderVo.getUse_start_time() + "至" + orderVo.getUse_end_time());
					llAvailableTime.setVisibility(View.VISIBLE);
				} else {
					if (Constants.ZIQU.equals(detailVo.getDraw_type())) {// 1是自取类型的
						tvConvertMethod.setText("自取");
						llGoodsFreight.setVisibility(View.GONE);
						llAvailableTime.setVisibility(View.VISIBLE);
						if (null == detailVo.getEnd_receipt()) {
							tvAvailableTime.setText("有效期至：暂无");
						} else {
							tvAvailableTime.setText("有效期至：" + detailVo.getEnd_receipt());
						}
						llExpressInfo.setVisibility(View.GONE);
						// 自取地址
						rlLookziqu.setVisibility(View.VISIBLE);
						if (null != indentDetailListVo.getList() && indentDetailListVo.getList().size() > 0) {
							View convertView = LayoutInflater.from(this).inflate(R.layout.shop_item_ziqu_address, null);
							TextView tvAddress = ViewHolder.getViewById(convertView, R.id.tv_address);
							TextView tvTime = ViewHolder.getViewById(convertView, R.id.tv_time);
							TextView tvContact = ViewHolder.getViewById(convertView, R.id.tv_contact);
							TextView tvContactPhone = ViewHolder.getViewById(convertView, R.id.tv_contact_phone);
							TextView tvNote = ViewHolder.getViewById(convertView, R.id.tv_note);
							LinearLayout llCallPhone = ViewHolder.getViewById(convertView, R.id.ll_callphone);
							llCallPhone.setVisibility(View.GONE);
							AddressVo entity = indentDetailListVo.getList().get(0);
							tvAddress.setText(String.format("地址：%s", entity.getAddress()));
							tvTime.setText(String.format("时间：%s", entity.getDraw_time()));
							tvContact.setText(String.format("联系人：%s", entity.getConnect_name()));
							tvContactPhone.setText(String.format("电话：%s", entity.getConnect_tel()));
							tvNote.setText(String.format("备注：%s", entity.getNote()));
							if (null != tempView) {
								llAddressInfo.removeView(tempView);
							}
							llAddressInfo.addView(convertView);
							tempView = convertView;
						}
					} else {// 快递
						rlLookziqu.setVisibility(View.GONE);
						View convertView = LayoutInflater.from(this).inflate(R.layout.shop_layout_address_info, null);
						// 收件人
						TextView recipients = ViewHolder.getViewById(convertView, R.id.recipients);
						// 联系电话
						TextView connectPhone = ViewHolder.getViewById(convertView, R.id.connect_phone);
						// 收货地址
						TextView address = ViewHolder.getViewById(convertView, R.id.address);
						recipients.setText(detailVo.getConnect_name());
						connectPhone.setText(detailVo.getConnect());
						address.setText(detailVo.getAddress());
						if (null != tempView) {
							llAddressInfo.removeView(tempView);
						}
						llAddressInfo.addView(convertView);
						tempView = convertView;

						tvConvertMethod.setText("快递");
						llGoodsReal.setVisibility(View.VISIBLE);
						llAvailableTime.setVisibility(View.GONE);
					}
				}
				BitmapUtils bitmapUtils = BitmapHelp.getBitmapUtils(this);
				bitmapUtils.display(goodsImg, detailVo.getImg_url());
			}
		}

	}

	private OnAntiViolenceClickListener clickListener = new OnAntiViolenceClickListener() {
		@Override
		public void onAVClick(View v) {
			int id = v.getId();
			if (id == R.id.btnOperation) {
				switch (Integer.parseInt(indentDetailListVo.getInfo().getStatus())) {
				case Constants.UNPAID_CODE:// 未支付
					Bundle bundle = new Bundle();
					bundle.putString("goodsType", orderVo.getType());
					bundle.putString("orderNum", orderVo.getOrder_num());
					bundle.putString("goodsName", orderVo.getGoods_name());
					bundle.putString("submitType", orderVo.getDraw_type());
					bundle.putString("goodsImg", orderVo.getImg_url());
					bundle.putInt("payType", payType);
					toActivity(PayforActivity.class, bundle);
					break;
				case Constants.UNFILLEDORDER_CODE:// 未发货 未领取
					if (TextUtil.stringIsNotNull(Constants.serviceTel)) {
						refundDialog.show();
					}
					break;
				case Constants.SHIPPED_CODE:// 确认收货
					sweetAlertDialog.setTitleText("为确保商品准确送到您手中\n亲，您真的收到商品了吗").setCancelText("取消").showCancelButton(true)
							.setConfirmText("确定").setConfirmClickListener(new OnSweetClickListener() {
								@Override
								public void onClick(SweetAlertDialog sweetAlertDialog) {
									sweetAlertDialog.dismiss();
									confirmGoodsReceive(orderVo);
								}
							}).show();
					break;
				case Constants.TAKEGOODS_CODE:// 待评价
					Intent intent = new Intent(OrderDetailActivity.this, AddCommentActivity.class);
					intent.putExtra(AddCommentActivity.ID, orderVo.getGoods_id());
					intent.putExtra(AddCommentActivity.ORDER_NUM, orderVo.getOrder_num());
					intent.putExtra(AddCommentActivity.TYPE, "1");
					intent.putExtra(AddCommentActivity.NAME, orderVo.getGoods_name());
					intent.putExtra(AddCommentActivity.IMG_URL, orderVo.getImg_url());
					intent.putExtra(AddCommentActivity.CASH_PAY, orderVo.getCash_pay());
					intent.putExtra(AddCommentActivity.PRICE_PAY, orderVo.getPrice_pay());
					startActivityForResult(intent, 0);
					overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					break;
				case Constants.FINISH_CODE:
				case Constants.CANCEL_CODE:
				case Constants.PAST_CODE:
				case Constants.CLOSE_CODE:
					new SweetAlertDialog(OrderDetailActivity.this).setTitleText("确定要删除订单吗").setCancelText("取消").showCancelButton(true)
							.setConfirmText("确定").setConfirmClickListener(new OnSweetClickListener() {
								@Override
								public void onClick(SweetAlertDialog sweetAlertDialog) {
									sweetAlertDialog.dismiss();
									deleteOrder(orderVo);
								}
							}).show();
					break;
				}
			} else if (id == R.id.shop_detail_btn_cancel_indent) {
				switch (Integer.parseInt(indentDetailListVo.getInfo().getStatus())) {
				case Constants.UNPAID_CODE:// 未支付
					sweetAlertDialog.setTitleText("确定要取消订单吗?").setCancelText("取消").showCancelButton(true).setConfirmText("确定")
							.setConfirmClickListener(new OnSweetClickListener() {
								@Override
								public void onClick(SweetAlertDialog sweetAlertDialog) {
									sweetAlertDialog.dismiss();
									cancelOrder(orderVo);
								}
							}).show();
					break;
				case Constants.TAKEGOODS_CODE:
				case Constants.FINISH_CODE:
				case Constants.SHIPPED_CODE:
				case Constants.PAST_CODE:
					if (TextUtil.stringIsNotNull(Constants.serviceTel)) {
						refundDialog.show();
					}
					break;
				}
			} else if (id == R.id.goodsInfoLayout || id == R.id.commodity_tv_title || id == R.id.commodity_iv_img) {
				Bundle bundle = new Bundle();
				bundle.putString("goodsId", indentDetailListVo.getInfo().getGoods_id());
				toActivity(GoodsDetailActivity.class, bundle);
			} else if (id == R.id.btn_lookalladdress) {
				Bundle bundle = new Bundle();
				bundle.putString("pId", orderVo.getP_id());
				bundle.putString("shopId", orderVo.getShop_id());
				bundle.putString("goodsId", orderVo.getGoods_id());
				toActivity(ZiquAddressActivity.class, bundle);
			} else {
			}
		}
	};

	private void loadData(final OrderVo orderVo) {
		// 加載客服電話
		loadTelNum();
		final ProgressDialog progressDialog = new ProgressDialog(this);
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "201");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("order_num", orderVo.getOrder_num());
		params.addQueryStringParameter("user_id", Constants.userCenterId);
		params.addQueryStringParameter("draw_type", orderVo.getDraw_type());
		ApiDebugUtil.debug(params);
		HttpUtils http = new HttpUtils();
		http.send(HttpRequest.HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {
			@Override
			public void onStart() {
				try {
					progressDialog.show();
				} catch (Exception e) {
				}
				LogUtils.i(">>>>>>====" + this.getRequestUrl());
			}

			@Override
			public void onLoading(long total, long current, boolean isUploading) {
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				if (responseInfo != null && TextUtil.stringIsNotNull(responseInfo.result)) {
					LogUtils.e("responseInfo.result==>" + responseInfo.result);
					try {
						indentDetailListVo = JSON.parseObject(responseInfo.result, IndentDetailListVo.class);
					} catch (Exception e) {
						e.printStackTrace();
						showShortToast("数据解析出错");
						return;
					}
					setView(orderVo, indentDetailListVo);
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				progressDialog.dismiss();
				Toast.makeText(OrderDetailActivity.this, "获取数据失败:msg=" + msg, 0).show();
			}
		});

	}

	// 拨打电话
	@Override
	public void btnCallClick() {
		refundDialog.dismiss();
		// 跳到拨号界面
		Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Constants.serviceTel));
		startActivity(intent);
	}

	// 加载客服电话
	private void loadTelNum() {
		if (TextUtil.stringIsNull(Constants.serviceTel)) {
			RequestParams params = new RequestParams();
			params.addQueryStringParameter("por", "208");
			params.addQueryStringParameter("proIden", Constants.STOREFLAG);
			HttpUtils http = new HttpUtils();
			http.send(HttpRequest.HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					if (responseInfo != null && TextUtil.stringIsNotNull(responseInfo.result)) {
						LogUtils.e("responseInfo.result==>" + responseInfo.result);
						ServiceTelVo result = JSON.parseObject(responseInfo.result, ServiceTelVo.class);
						if (null == result) {
							return;
						}
						Constants.serviceTel = result.getPhone();
						initRefundDialog();
					}
				}

				@Override
				public void onFailure(HttpException error, String msg) {
				}
			});
		} else {
			initRefundDialog();
		}

	}

	private void initRefundDialog() {
		if (refundDialog == null) {
			refundDialog = new RefundDialog(OrderDetailActivity.this);
			refundDialog.setListener(OrderDetailActivity.this);
			refundDialog.setTellNum(Constants.serviceTel);
		}
	}

	private void cancelOrder(final OrderVo orderVo) {
		final ProgressDialog progressDialog = new ProgressDialog(this);
		OrderManager orderManager = new OrderManager();
		orderManager.setCallBack(new OrderOperationCallBack() {

			@Override
			public void onSuccess(BaseVo baseVo) {
				indentDetailListVo.getInfo().setStatus(orderVo.getStatus());
				setView(orderVo, indentDetailListVo);
				int status = 0;
				try {
					status = Integer.valueOf(orderVo.getStatus());
					// setResult(status);
					if (OrderListActivity.handler != null) {
						OrderListActivity.handler.sendEmptyMessage(status);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onRequestStart() {
				try {
					progressDialog.show();
				} catch (Exception e) {
				}

			}

			@Override
			public void onRequestComplete() {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onFailure(BaseVo baseVo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				Toast.makeText(OrderDetailActivity.this, "请求数据失败:" + msg, 0).show();
			}
		});
		orderManager.cancelOrder(orderVo);
	}

	private void deleteOrder(final OrderVo orderVo) {
		final ProgressDialog progressDialog = new ProgressDialog(this);
		OrderManager orderManager = new OrderManager();
		orderManager.setCallBack(new OrderOperationCallBack() {

			@Override
			public void onSuccess(BaseVo baseVo) {
				// TODO removeItem(orderVo); 删除订单可以吗？
				int status = -1;
				// setResult(status);
				if (OrderListActivity.handler != null) {
					OrderListActivity.handler.sendEmptyMessage(status);
				}
				finish();
			}

			@Override
			public void onRequestStart() {
				try {
					progressDialog.show();
				} catch (Exception e) {
				}

			}

			@Override
			public void onRequestComplete() {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}

			}

			@Override
			public void onFailure(BaseVo baseVo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				Toast.makeText(OrderDetailActivity.this, "请求数据失败:" + msg, 0).show();
			}
		});
		orderManager.deleteOrder(orderVo);

	}

	/**
	 * 
	 * @author zrh
	 * @methods confirmGoodsReceive
	 * @description 确认收货
	 * @date 2015年1月16日 下午6:25:31 参数说明
	 */
	private void confirmGoodsReceive(final OrderVo orderVo) {
		final ProgressDialog progressDialog = new ProgressDialog(this);
		OrderManager orderManager = new OrderManager();
		orderManager.setCallBack(new OrderOperationCallBack() {

			@Override
			public void onSuccess(BaseVo baseVo) {
				indentDetailListVo.getInfo().setStatus(orderVo.getStatus());
				setView(orderVo, indentDetailListVo);
				int status = 0;
				try {
					status = Integer.valueOf(orderVo.getStatus());
					// setResult(status);
					if (OrderListActivity.handler != null) {
						OrderListActivity.handler.sendEmptyMessage(status);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onRequestStart() {
				try {
					progressDialog.show();
				} catch (Exception e) {
				}
			}

			@Override
			public void onRequestComplete() {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onFailure(BaseVo baseVo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				Toast.makeText(OrderDetailActivity.this, "请求数据失败:" + msg, 0).show();
			}
		});
		orderManager.confirmGoodsReceive(orderVo);

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		switch (arg0) {
		case 0:// 评论页面 返回
			if (arg1 == RESULT_OK) {
				loadData(orderVo);
			}
			break;

		default:
			break;
		}
	}
}
